;define(function(){
	
	function build(settings){
		var $obj = $("<div class='titleShow'>").empty();
			$obj.data("info",settings);
		var content = "";
		if(settings.appendTo){
			settings.appendTo.empty().append($obj);
			if(settings.autoStationPro){
				var time = settings.autoStationPro.time;
				var pro = settings.autoStationPro.pro;
				if($.isArray(pro) && pro.length>0){
					content += "自动站站点[";
					for(var i=0;i<pro.length;i++){
						if(i==(pro.length-1)){
							content += pro[i].title + (pro[i].unit ? ("(" + pro[i].unit + ")") : " ");
						}else{
							content += pro[i].title + (pro[i].unit ? ("(" + pro[i].unit + ")") : " ") + "+";
						}
					}
					content += "]" + "<span style=\"font-family:'Arial'\" item='1' key=''>" + time.substring(11,16) + "</span>";
				}
			}
			
			if(settings.GDPro && settings.GDPro.length>0){
				if(content){
					content = content + "+";
				}
				for(var j=0;j<settings.GDPro.length;j++){
					var each = settings.GDPro[j];
					if(j==(settings.GDPro.length-1)){
						content += each.title+(each.unit ? ("(" + each.unit + ")") : " ")+ "<span style=\"font-family:'Arial'\" item='0' key='"+each.key+"'>" + each.time.substring(11,16) + "</span>";
					}else{
						content += each.title+(each.unit ? ("(" + each.unit + ")") : " ")+ "<span style=\"font-family:'Arial'\" item='0' key='"+each.key+"'>" + each.time.substring(11,16) + "</span>" + "+";
					}
				}
			}
		}
		$obj.html(content);
		$obj.css({
			"width" : '700px',
			"line-height" : "30px",
			'color' : '#333',
			'font-size' : '16px',
			'font-weight': 'bold',
			'text-align' : 'center'
		})
		if(settings.showOrHide == 'hide'){
			$obj.hide();	
		}
	}
	
	//obj-标题对象  proObj-添加产品
	function addOne(obj,proObj){
		var settings = obj.data("info");
		if(proObj.isAuto){
			if(settings.autoStationPro){
				var hasInfo = $.grep(settings.autoStationPro.pro, function(n,i){
				    return n.key == proObj.key;
				});
				if(hasInfo.length==0){
					settings.autoStationPro.pro.push(proObj);
				}else{
					return
				}
			}else{
				var proInfo = {
					time : proObj.time,
					pro : [proObj]
				} 
				settings.autoStationPro = proInfo;
			}
		}else{
			if(settings.GDPro){
				var hasInfo = $.grep(settings.GDPro, function(n,i){
				    return n.key == proObj.key;
				});
				if(hasInfo.length==0){
					settings.GDPro.push(proObj);
				}else{
					return
				}
			}else{
				settings.GDPro = [proObj];
			}
		}
		settings.showOrHide = "show";
		build(settings);
	}
	
	//obj-标题对象  key-产品标识
	function deleteOne(obj,key,isAuto){
		var settings = obj.data("info");
		if(isAuto){
			if(settings.autoStationPro){
				var pro = settings.autoStationPro.pro;
				var newPro = [];
				for(var i=0;i<pro.length;i++){
					if(key != pro[i].key){
						newPro.push(pro[i]);
					}
				}
				settings.autoStationPro.pro = newPro;
			}
		}else{
			if(settings.GDPro){
				var newGDPro = [];
				for(var j=0;j<settings.GDPro.length;j++){
					if(settings.GDPro[j].key != key){
						newGDPro.push(settings.GDPro[j]);
					}
				}
				settings.GDPro = newGDPro;
			}
		}
		settings.showOrHide = "show";
		build(settings);
	}
	
	var titleShow = function(settings){
		var options = {
			'appendTo' : null,			//容器
			'autoStationPro' : null,	//自动站站点信息    {time:'',pro:[{title : "",unit : "",key:''}]}
			'GDPro' : null,				//格点显示产品  [{time:'',title:'',unit:'',key:''}]
			'showOrHide' : 'hide',		//显示隐藏 （show/hide） 
		}
		this.settings = $.extend(options,settings);
		build(this.settings);
	}
	
	titleShow.prototype = {
		addPro : function(obj){					//{time:'',title:'',unit:'',key:'',isAuto:true/false}  	isAuto--是否为自动站站点的数据
			addOne($(".titleShow"),obj);
		},
		deletePro : function(key,isAuto){		//isAuto:true/false  isAuto--是否为自动站站点的数据
			if(key.includes(",")){
				var keys = key.split(",");
				for(var i=0;i<keys.length;i++){
					if(keys[i]){
						deleteOne($(".titleShow"),keys[i],isAuto);
					}
				}
			}else{
				deleteOne($(".titleShow"),key,isAuto);
			}
		},
		changeTime : function(time,isAuto,key){    // key - 要素     time - 时间    isAuto:true/false  isAuto--是否为自动站站点的数据
			var item = isAuto ? "1" : "0";
			var dataInfo = $(".titleShow").data("info");
			if(isAuto){
				$(".titleShow").find("span[item="+item+"]").text(time.substring(11,16));
			}else{
				$(".titleShow").find("span[item="+item+"][key='"+key+"']").text(time.substring(11,16));
				var GDPro = dataInfo.GDPro;
				for(var i=0;i<GDPro.length;i++){
					if(GDPro[i].key == key){
						GDPro[i].time = time;
					}
				}
				dataInfo.GDPro = GDPro;
				$(".titleShow").data("info",dataInfo);
			}
		},
		hide : function(){
			$(".titleShow").hide();
		},
		show : function(){
			$(".titleShow").show();
		}
	};
	
	CafeJS.TitleShow = function(settings){
		var drop = new titleShow(settings);
		return drop;
	}
});